package com.example.util;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.servlet.ServletOutputStream;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AesForFile {
    public static void encryptedFile(String originFileName, String encryptedFileName, SecretKey key) {
        try {
            // 把文件转的字节数组
            File originFile = new File(originFileName);
            FileInputStream fis = new FileInputStream(originFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            // 缓冲区
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = fis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            // 得到的字节数组
            byte[] fileByte = bos.toByteArray();

            // AES加密后的字节数组
            byte[] encryptByte = AESUtil.encrypt(fileByte, key);
            // 将加密后的bytes 转换为 file
            File encryptedFile = new File(encryptedFileName);
            FileOutputStream fos = new FileOutputStream(encryptedFile);
            fos.write(encryptByte, 0, encryptByte.length);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param is  获取到的输入流
     * @param fos 获取到的输出流
     * @param key 传入的key
     */
    public static void encryptedFile(InputStream is, FileOutputStream fos, SecretKey key) {
        try {
            // 把文件转的字节数组
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            // 缓冲区
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = is.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            // 得到的字节数组
            byte[] fileByte = bos.toByteArray();

            // AES加密后的字节数组
            byte[] encryptByte = AESUtil.encrypt(fileByte, key);
            // 将加密后的bytes 转换为 file
            fos.write(encryptByte, 0, encryptByte.length);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }

    public static void decryptedFile(String originFileName, String encryptedFileName, SecretKey key) {
        try {
            // 把文件转成byte[]
            File originFile = new File(originFileName);
            FileInputStream fis = new FileInputStream(originFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = fis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            // 得到的字节数组
            byte[] fileByte = bos.toByteArray();

            // AES解密后的字节数组
            byte[] encryptByte = AESUtil.decrypt(fileByte, key);
            // 将解密后的bytes 转换为 file
            File encryptedFile = new File(encryptedFileName);
            FileOutputStream fos = new FileOutputStream(encryptedFile);
            fos.write(encryptByte, 0, encryptByte.length);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }

    public static void decryptedFile(byte[] originFileBytes, ServletOutputStream os, SecretKey key) {
        try {
            // AES解密后的字节数组
            byte[] encryptByte = AESUtil.decrypt(originFileBytes, key);
            os.write(encryptByte, 0, encryptByte.length);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }
}
